package com.ubix.kiosoft2;


import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.ubix.kiosoft2.TestUtils.sleep;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class ReloadBtnTest {

    @Rule
    public ActivityTestRule<SrcActivity> mActivityTestRule = new ActivityTestRule<>(SrcActivity.class);

    @Test
    public void reloadBtnTest() {
        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.reg_start), withText("Scan QR Code"), isDisplayed()));
        appCompatButton.perform(click());

        sleep(10000);

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.signin_email), isDisplayed()));
        appCompatEditText.perform(click());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.signin_email), isDisplayed()));
        appCompatEditText3.perform(replaceText("bb@bb.com"), closeSoftKeyboard());

        ViewInteraction appCompatEditText4 = onView(
                allOf(withId(R.id.signin_password), isDisplayed()));
        appCompatEditText4.perform(replaceText("111111"), closeSoftKeyboard());

        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.signin_signin_btn), withText("SIGN IN"), isDisplayed()));
        appCompatButton2.perform(click());

        sleep(3000);

        ViewInteraction appCompatImageView = onView(
                allOf(withId(R.id.main_reload_btn), isDisplayed()));
        appCompatImageView.perform(click());
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
